package com.swind.fast.modules.app.halali.partner.controller;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.common.constant.CommonConstant;
import com.swind.fast.modules.app.halali.partner.entity.Order;
import com.swind.fast.modules.app.halali.partner.entity.OrderItem;
import com.swind.fast.modules.app.halali.partner.service.IOrderService;

import com.swind.fast.modules.app.halali.partner.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.swind.fast.common.system.base.controller.SwindController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
// import com.swind.fast.common.aspect.annotation.AutoLog;

import com.swind.fast.modules.system.entity.SysAnnouncement;
import com.swind.fast.modules.system.entity.SysAnnouncementSend;
import com.swind.fast.modules.system.service.ISysAnnouncementSendService;
import com.swind.fast.modules.system.service.ISysAnnouncementService;
import java.util.Optional;
import java.text.DecimalFormat;

import org.apache.shiro.SecurityUtils;
import com.swind.fast.common.system.vo.HalaliLoginUser;
import com.swind.fast.common.system.vo.LoginUser;
import com.swind.fast.modules.app.halali.partner.vo.request.OrderVo;
import com.swind.fast.modules.app.halali.partner.vo.request.OrderStateVo;
import com.swind.fast.modules.app.halali.partner.vo.response.OrderDetailVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

import com.swind.fast.modules.app.halali.common.constant.enums.OrderStatus;

 /**
 * @Description: 餐厅
 * @Author: swind
 * @Date:   2022-01-20
 * @Version: V1.0
 */
@Api(tags="03.商户端-订单")
@RestController
@RequestMapping("/app/partner/order")
@Slf4j
public class OrderController extends SwindController<Order, IOrderService> {
	@Autowired
	private IOrderService orderService;

	@Autowired
	private IRestaurantMealService restaurantMealService;

	@ApiOperation(value="订单-列表", notes="")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
							@ApiParam(name ="historical",type="boolean",value="是否为历史订单",example="1",required=false)
    							   @RequestParam(name="historical", defaultValue="0") Boolean historical,
                            @ApiParam(name ="status",type="int",value="订单状态(1-New,2-Preparing,3-Ready,4-Completed)",example="1",required=false)
                                   @RequestParam(name="status", defaultValue="0") Integer status,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		HalaliLoginUser sysUser = (HalaliLoginUser) SecurityUtils.getSubject().getPrincipal();
		String restId = sysUser.getRestaurantId();
		if (oConvertUtils.isEmpty(restId)) {
			Page<Order> page = new Page<Order>(pageNo, pageSize);
			return Result.OK(page);
		}

        OrderStatus orderStatus = OrderStatus.getOrderStatusByCode(status);
        if (oConvertUtils.isEmpty(orderStatus)) {
            orderStatus = OrderStatus.NEW;
        }


		LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getRestaurantId, restId);
        queryWrapper.eq(Order::getState, orderStatus.getCode());
        LocalDateTime startOfToday = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        if (!historical) {
        	queryWrapper.gt(Order::getCreateTime, startOfToday);
        } else {
        	queryWrapper.lt(Order::getCreateTime, startOfToday);
        }

		Page<Order> page = new Page<Order>(pageNo, pageSize);
		IPage<Order> pageList = orderService.page(page, queryWrapper);
		pageList = orderService.renderPage(pageList);
		return Result.OK(pageList);
	}

	@ApiOperation(value="订单-详情", notes="")
	@GetMapping(value = "/detail")
	public Result<?> detail(@RequestParam(name="id", required=true) String id) {
		OrderDetailVo orderDetail = orderService.getDetailById(id);
		return Result.OK(orderDetail);
	}

    @ApiOperation(value="订单-更新", notes="")
    @PostMapping(value = "/update/state")
    public Result<Order> updateStateAction(@RequestBody OrderStateVo orderInfo) {

        Order order = this.service.updateState(orderInfo);
        return Result.OK(order);
    }

}
